package xing2387.filtervideoplayer.player;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;

import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.analytics.AnalyticsListener;
import com.google.android.exoplayer2.video.VideoListener;

import xing2387.filtervideoplayer.filter.GlFilter;
import xing2387.filtervideoplayer.player.gl.EConfigChooser;
import xing2387.filtervideoplayer.player.gl.EContextFactory;
import xing2387.filtervideoplayer.player.gl.GLPlayerRenderer;

/**
 * Created by sudamasayuki on 2017/05/16.
 */
public class GLPlayerView extends GLSurfaceView implements VideoListener {

    private final static String TAG = GLPlayerView.class.getSimpleName();

    private final GLPlayerRenderer renderer;
    private SimpleExoPlayer player;
    private boolean resumed;

    private float videoAspect = 1f;

    public GLPlayerView(Context context) {
        this(context, null);
    }

    public GLPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setEGLContextFactory(new EContextFactory());
        setEGLConfigChooser(new EConfigChooser());

        renderer = new GLPlayerRenderer(this);
        setRenderer(renderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    public GLPlayerView setSimpleExoPlayer(SimpleExoPlayer player) {
//        if (this.player != null) {
//            this.player.release();
//            this.player = null;
//        }
        this.player = player;
//        player.addAnalyticsListener(new AnalyticsListener() {
//            @Override
//            public void onSeekStarted(EventTime eventTime) {
//                Log.d(TAG, "onSeekStarted: " + eventTime.eventPlaybackPositionMs);
//            }
//        });
        this.player.addVideoListener(this);
        this.renderer.setSimpleExoPlayer(player);
        onResume();
        return this;
    }

    private void detachPlayer() {
        if (player != null) {
            player.removeVideoListener(this);
            renderer.detachPlayer();
            player = null;
        }
        onPause();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        detachPlayer();
    }

    public void setGlFilter(GlFilter glFilter) {
        renderer.setGlFilter(glFilter);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void onResume() {
        if (!resumed) {
            super.onResume();
            Log.d(TAG, hashCode() + " onResume: ");
        }
    }

    @Override
    public void onPause() {
        if (resumed) {
            super.onPause();
            Log.d(TAG, hashCode() + " onPause: ");
        }
    }

    @Override
    public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
        Log.d(TAG, hashCode() + " onVideoSizeChanged: " +
                "width = " + width + " height = " + height +
                " unappliedRotationDegrees = " + unappliedRotationDegrees + " pixelWidthHeightRatio = " + pixelWidthHeightRatio);
        videoAspect = ((float) width / height) * pixelWidthHeightRatio;
        renderer.setVideoSize(width, height);
        // Log.d(TAG, "videoAspect = " + videoAspect);
//        requestLayout();
    }

    @Override
    public void onRenderedFirstFrame() {
        Log.d(TAG, hashCode() + " onRenderedFirstFrame: ");
    }
}
